<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Users extends MY_Controller
{

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *        http://example.com/index.php/welcome
     *    - or -
     *        http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */

    public function __construct()
    {
        parent::__construct();
        $this->auth('admin');
        $this->_add_style_sheet("dataTables", base_url("assets/DataTables/css/dataTables.bootstrap4.min.css"));
        $this->_add_script_file("dataTables", base_url("assets/DataTables/js/jquery.dataTables.min.js"));
        $this->_add_script_file("dataTables Bootstrap", base_url("assets/DataTables/js/dataTables.bootstrap.min.js"));

        $this->load->model('users_model');

    }

    public function index()
    {
        $data = array();
        $this->_view('admin/users/users.twig', $data , 'admin','admin/users');
    }

    public function json()
    {
        $json = array();
        $data = $this->users_model->get_all('users');
        foreach ($data as $key => $value) {
            switch ($value['role']) {
                case 1:
                    $data[$key]['role'] = "root";
                    break;
                case 2:
                    $data[$key]['role'] = "超级管理员";
                    break;
                case 3:
                    $data[$key]['role'] = "管理员";
                    break;
                case 4:
                    $data[$key]['role'] = "认证用户";
                    break;
                case 5:
                    $data[$key]['role'] = "注册用户";
                    break;
                default:
                    $data[$key]['role'] = "游客";
                    break;
            }
        }
        $json["data"] = &$data;
        print_r(json_encode($json));
    }

    public function add()
    {
        $data = array();
        if ($this->input->post()) {
            $this->load->library('form_validation');
            $return = array();
            if ($this->form_validation->run('users') == FALSE) {
                $return['state'] = false;
                $this->form_validation->set_error_delimiters('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>', '</div>'); //去掉错误信息的<p>定界符
                $return['data'] = validation_errors();
            } else {
                $post = $this->input->post();
                if ($this->users_model->add('users', $post)) {
                    $return['state'] = true;
                    $return['data'] = $this->classesBg('success', '新增用户成功！');
                } else {
                    $return['state'] = false;
                    $return['data'] = $this->classesBg('dismissible', '数据更新失败！');
                }
            }
            print_r(json_encode($return));//输出json
        } else {
            $this->_view('admin/users/users-add.twig', $data);
        }
    }

    public function change($id = '')
    {
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('', ''); //去掉错误信息的<p>定界符
        $return = array();
        if ($this->form_validation->run('users') == FALSE) {
            $return['state'] = false;
            $return['data'] = validation_errors();
        } else {
            $post = $this->input->post();
            if ($this->users_model->update('users', $id, $post)) {
                $return['state'] = true;
            } else {
                $return['state'] = false;
                $return['data'] = '数据更新失败!';
            }
        }
        print_r(json_encode($return));
    }

    //暂时停用
    public function edit($id = '')
    {
        $data = array();
        $user = $this->users_model->get_user($id);
        if (empty($user)) {
            die('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>该用户不存在!</div>');
        }
        if ($this->input->post()) {
            $data = array(
                'name' => $this->input->post('name'),
                'role' => $this->input->post('role')
            );
            if ($this->users_model->update_user($id, $data)) {
                exit('<div class="alert alert-success alert-dismissible"><i class="icon fa fa-check"></i>修改成功！</div>');
            } else {
                die('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>修改失败，数据更新失败!</div>');
            }
        } else {
            $data['input'] = &$user;
            $this->_view('admin/users-edit.twig', $data);
        }
    }

    public function del($id = '')
    {
        $data = array();
        $user = $this->users_model->get('users', $id);
        if (empty($user)) {
            exit('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>严重错误，该用户不存在!</div>');
        }
        if ($this->input->post()) {
            $return = array();
            if ($this->users_model->del('users', $id)) {
                $return['state'] = true;
                $return['data'] = '删除成功！';
            } else {
                $return['state'] = false;
                $return['data'] = $this->classesBg('dismissible', '数据更新失败！');
            }
            print_r(json_encode($return));
        } else {
            $data['input'] = &$user;
            $this->_view('admin/users/users-del.twig', $data);
        }
    }
}
